import {Layer} from "../layer/Layer";
import {Source} from "../source/Source";
import {MapRenderer} from "../render/MapRenderer";
import {CRS} from "../crs/CRS";
import {CRS3857} from "../crs/CRS3857";
import {EventDispatcher, OBJLoader, TextureLoader} from "three";
import {Handler} from "../handler/Handler";
import {DragHandler} from "../handler/DragHandler";
import {WheelHandler} from "../handler/WheelHandler";
import {FreeMapConfig} from "./FreeMapConfig";
import {LatLng} from "../";
import {GesturableHandler} from "../handler/GesturableHandler";

/**
 * 地图对象
 */
export class FreeMap {
    public layers: Array<Layer> = [];

    public sources: Array<Source> = [];

    public handlers: Array<Handler> = [];

    public container: HTMLElement;

    public mapRenderer: MapRenderer;

    public crs: CRS;

    public eventDispatcher: EventDispatcher;

    public config: FreeMapConfig;

    constructor(id: string, config?: FreeMapConfig) {
        this.eventDispatcher = new EventDispatcher();
        this.initConfig(config);
        this.crs = new CRS3857();
        this.container = document.getElementById(id);
        this.mapRenderer = new MapRenderer(this.container, this.crs, this);
        this.initHandler();
    }

    private initConfig(config = {
        backGroundColor: '#f5f5f5',
        center: new LatLng(0, 0)
    } as FreeMapConfig) {
        this.config = config;
        if (!this.config.backGroundColor) {
            this.config.backGroundColor = '#f5f5f5';
        }
        if (!this.config.center) {
            this.config.center = new LatLng(0, 0);
        }
    }

    private initHandler() {
        this.handlers.push(new DragHandler(this));
        this.handlers.push(new WheelHandler(this));
        this.handlers.push(new GesturableHandler(this));
    }

    public addSource(source: Source) {
        source.addTo(this);
        this.sources.push(source);
    }

    public addLayer(layer: Layer) {
        layer.addTo(this);
        this.layers.push(layer);
    }
}
